<?php
/**
 *   Joomdiet - components/views/menuinfotable/view.html.php
 *   Author: Juan Dapena Paz (juan@bittingbits.com)
 *   Copyright (C) 2010 Bitting Bits S.L.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.view');

class JoomdietViewMenuInfoTable extends JView
{
    function display($tpl=null)
    {
        $menu = JoomdietHelperFrontMenu::getInstance();
        echo $menu->display();
    
        $recipe_food_model = $this->getModel('RecipeFood');
        $recipe_model = $this->getModel();
        $food_variable_model = $this->getModel('FoodVariable');
        $variable_model = $this->getModel('Variable');
        
        $recipeids_string = JRequest::getVar('recipes');
        if ($recipeids_string && ($recipeids_string != '0'))
            $ids_array = explode(' ', $recipeids_string);
        else $ids_array = array();
        
        $foodids_string = JRequest::getVar('foods');
        if ($foodids_string && ($foodids_string != '0')) 
            $foodids_array = explode(' ', $foodids_string);
        else $foodids_array = array();
        
        $results = array();
        
        foreach ($ids_array as $id)
        {
            $recipe = $recipe_model->getRecipe($id);
            
            $recipe_foods = $recipe_food_model->getData($id);
            foreach ($recipe_foods as $recipe_food)
            {
                $amount = $recipe_food->amount;
                $multiplier = $amount/100;
                $variables = $food_variable_model->getFoodVariables($recipe_food->id_food);
                foreach ($variables as $variable)
                {
                    $found = false;
                    foreach ($results as $k => $result) {
                        if ($result['id'] == $variable->id_variable) {
                            $found = true;
                            $results[$k]['amount'] += $variable->amount*$multiplier;
                        }
                    }
                    if (!$found) {
                        $result = array();
                        $result['id'] = $variable->id_variable;
                        $var_data = $variable_model->getVariable($variable->id_variable);
                        $result['name'] = $var_data->name;
                        $result['unit'] = $var_data->unit;
                        $result['amount'] = $variable->amount;
                        $results[] = $result;
                    }
                }
            }
        }
        
        foreach ($foodids_array as $id) {
            $amount = JRequest::getVar('amount'.$id);
            $multiplier = $amount/100;
            $variables = $food_variable_model->getFoodVariables($id);
            foreach ($variables as $variable)
            {
                $found = false;
                foreach ($results as $k => $result) {
                    if ($result['id'] == $variable->id_variable) {
                        $found = true;
                        $results[$k]['amount'] += $variable->amount*$multiplier;
                    }
                }
                if (!$found) {
                    $result = array();
                    $result['id'] = $variable->id_variable;
                    $var_data = $variable_model->getVariable($variable->id_variable);
                    $result['name'] = $var_data->name;
                    $result['unit'] = $var_data->unit;
                    $result['amount'] = $variable->amount;
                    $results[] = $result;
                }
            }
        }
        
        $this->assignRef('results', $results);
        parent::display($tpl);
    }
}
?>
